/*********************************************************************************
 *      Copyright:  (C) 2022 YanlinZhou<1222>
 *                  All rights reserved.
 *
 *       Filename:  getopt.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(04/09/2022)
 *         Author:  YanlinZhou <1222>
 *      ChangeLog:  1, Release initial version on "04/09/2022 10:48:11 AM"
 *                 
 ********************************************************************************/

#include "socket.h"

/***************************Funtion declaration******************************
| * Function nam: print_server_usage
| * Funtion performance: to print the command line parameter parsing usage
| * Parameter: char *progname
| * Returned value: 1
| * Description: NULL
*****************************************************************************/
int print_server_usage(char *progname)
{
    printf("------------------ %s usage -----------------------\n", progname);
    printf("-i(--ipaddr):sepcify server IP address\n");
    printf("-p(--port):sepcify server port.\n");
    printf ("-d(--d):the progrom running in the background or not \n");
    printf("-h(--help):printf this help information.\n");
    printf ("---------------------end--------------------------\n");
    return 1;
}

/***************************Funtion declaration******************************
| * Function nam: socket_server_init
| * Funtion performance: socket init 
| * Parameter: char * listen_ip, char * listen_port
| * Returned value:conn_fd
| * Description: NULL
*****************************************************************************/
int socket_server_init(char *listen_ip, int listen_port)
{
	 struct sockaddr_in servaddr;
	 int rv = 0;
	 int on = 1;
	 int listenfd;

	 if ( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
	 {
		 printf("Use socket() to create a TCP socket failure: %s\n", strerror(errno));
		 return -1;
 	 }

	 /* Set socket port reuseable, fix 'Address already in use' bug when socket server restart */
	 setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));

	 memset(&servaddr, 0, sizeof(servaddr));
	 servaddr.sin_family = AF_INET;
	 servaddr.sin_port = htons(listen_port);
	 if( !listen_ip ) /* Listen all the local IP address */
	 {
	 	servaddr.sin_addr.s_addr = htonl(INADDR_ANY); 
	 }

	 else /* listen the specified IP address */
	 {
		 if (inet_pton(AF_INET, listen_ip, &servaddr.sin_addr) <= 0)
		 {
		 printf("inet_pton() set listen IP address failure.\n");
		 rv = -2;
		 goto CleanUp;
		 }
	 }

	 if( bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0 )
	 {
		 printf("Use bind() to bind the TCP socket failure: %s\n", strerror(errno));
		 rv = -3;
		 goto CleanUp;
	 }
	 if(listen(listenfd, 13) < 0)
	 {
		 printf("Use bind() to bind the TCP socket failure: %s\n", strerror(errno));
		 rv = -4;
		 goto CleanUp;
	 }

CleanUp:
		 if(rv<0)
		 close(listenfd);
		 else
		 rv = listenfd;
		 return rv;
}

